//
//  AlbumListViewController.m
//  Shanghai Cuisine
//
//  Created by iOS Two on 14/12/17.
//  Copyright (c) 2014年 iOS Two. All rights reserved.
//

#import "AlbumListViewController.h"
#import "InfoViewController.h"

@interface AlbumListViewController ()

@end

@implementation AlbumListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getASIData];
    [self creatController];
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 30)];
    titleLabel.text = @"文章推荐";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.font = [UIFont systemFontOfSize:19];
    titleLabel.textColor = [UIColor colorWithRed:0.97 green:0.69 blue:0.17 alpha:1];
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.97 green:0.69 blue:0.17 alpha:1];
    self.navigationItem.titleView = titleLabel;
    
    // Do any additional setup after loading the view.
}
-(void)creatController {
    _mainTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kwidth, kHeight - stateHight.size.height - navHight) style:UITableViewStylePlain];
    _mainTable.dataSource = self;
    _mainTable.delegate = self;
    _mainTable.rowHeight = 100;
    _mainTable.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1];
    _mainTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_mainTable];
}
-(void)getASIData {
    ASIHTTPRequest *request = [[ASIHTTPRequest alloc]initWithURL:[NSURL URLWithString:@"http://api.hoto.cn/index.php?appid=4&appkey=573bbd2fbd1a6bac082ff4727d952ba3&deviceid=0F607264FC6318A92B9E13C65DB7CD3C%7C7DB64D6A-18DB-4843-BAD9-0D906C652209%7CC73A6469-0555-4A64-AC37-3A540302FBD0&format=json&loguid=&method=Search.getAlbumList&sessionid=1418610742&uuid=353ED12DEEE49FC5F5DF6F2CB3EC5665&vc=31&vn=v4.5.0&keyword=%E4%B8%8A%E6%B5%B7%E8%8F%9C"]];
    request.username = @"1";
    request.delegate = self;
    [request startAsynchronous];
}
-(void)requestFinished:(ASIHTTPRequest *)request {
    _titleName = [[NSMutableArray alloc]init];
    _info = [[NSMutableArray alloc]init];
    _cover = [[NSMutableArray alloc]init];
    _albumId = [[NSMutableArray alloc]init];
    SBJsonParser *jsonP = [[SBJsonParser alloc]init];
    if ([request.username isEqualToString:@"1"]) {
        NSArray *array = [[[jsonP objectWithData:[request responseData]] objectForKey:@"result"] objectForKey:@"list"];
        for (int i = 0; i < array.count; i++) {
            [_cover addObject:[[array objectAtIndex:i] objectForKey:@"Cover"]];
            [_info addObject:[[array objectAtIndex:i] objectForKey:@"Intro"]];
            [_albumId addObject:[[array objectAtIndex:i] objectForKey:@"AlbumId"]];
            [_titleName addObject:[[array objectAtIndex:i] objectForKey:@"Title"]];
        }
        [_mainTable reloadData];
    }
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _info.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *string = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
    if(cell == nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
    } else {
        for (UIView *view in [cell.contentView subviews]) {
            [view removeFromSuperview];
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1];
    
    UIView *back = [[UIView alloc]initWithFrame:CGRectMake(3, 12, 312, 85)];
    back.backgroundColor = [UIColor whiteColor];
    back.layer.borderWidth = 0.5;
    back.layer.borderColor = [[UIColor colorWithRed:0.31 green:0.21 blue:0.15 alpha:1] CGColor];
    back.layer.shadowColor = [UIColor grayColor].CGColor;
    back.layer.shadowOffset = CGSizeMake(2, 2);
    back.layer.shadowOpacity = 0.5;
    back.layer.shadowRadius = 1.0;
    [cell.contentView addSubview:back];
    
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(18, 19, 72, 72)];
    [image setImageURLStr:[_cover objectAtIndex:indexPath.row] placeholder:[UIImage imageNamed:@"1_03 2"]];
    [cell.contentView addSubview:image];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(95, 16, 207, 20)];
    label.text = [_titleName objectAtIndex:indexPath.row];
    label.textColor = [UIColor colorWithRed:0.33 green:0.21 blue:0.14 alpha:1];
    label.font = [UIFont systemFontOfSize:15];
    label.textAlignment = NSTextAlignmentCenter;
    [cell.contentView addSubview:label];
    
    UIImageView *line = [[UIImageView alloc]initWithFrame:CGRectMake(97, 36, 207, 1)];
    line.backgroundColor = [UIColor colorWithRed:0.79 green:0.76 blue:0.75 alpha:1];
    [cell.contentView addSubview:line];
    
    UILabel *info = [[UILabel alloc]initWithFrame:CGRectMake(95, 42, 213, 49)];
    info.text = [_info objectAtIndex:indexPath.row];
    info.textColor = [UIColor colorWithRed:0.31 green:0.31 blue:0.31 alpha:1];
    info.font = [UIFont systemFontOfSize:13];
    info.numberOfLines = 3;
    [cell.contentView addSubview:info];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    InfoViewController *info = [[InfoViewController alloc]init];
    info.albumId = [_albumId objectAtIndex:indexPath.row];
    self.navigationItem.title = @"返回";
    [self.navigationController pushViewController:info animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
